#include "stdafx.h"
#include "Scene.h"

Scene::Scene(void)
{
	init();
}

Scene::~Scene(void)
{	
	for(int i = 0; i < objects.size() ; i++)
		delete objects[i];

	for (map<string, Material*>::iterator it = materials.begin(); it!=materials.end(); ++it)
		delete it->second;
}

void Scene::init(void)
{
	camera_eye.X = -31.4748; camera_eye.Y = 5.54301; camera_eye.Z = -2.33416;
	camera_forward.dX = 1.27483; camera_forward.dY = -0.0242219; camera_forward.dZ =-0.491444;
	camera_up.dX = 0; camera_up.dY = 1;	camera_up.dZ = 0;
	
	/*camera_eye.X = 0; camera_eye.Y = 0;	camera_eye.Z = 5;
	camera_forward.dX = 0; camera_forward.dY = 0; camera_forward.dZ = -1;
	camera_up.dX = 0; camera_up.dY = 1;	camera_up.dZ = 0;*/

	displayNormal = false; displaySilhouette = false; displayGooch = false; displayTexture = true;
	render_type = 1;
}

void Scene::render(void)
{
	if ( displaySilhouette )
		ShaderManager::changeShader(silhouette_Shaders);
	else if ( displayGooch )
		ShaderManager::changeShader(gooch_Shaders);
	else
	{
		glUniform1i(ShaderManager::loc_render_type, render_type);
		ShaderManager::changeShader(light_Shaders);
	}

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	gluLookAt(camera_eye.X, camera_eye.Y, camera_eye.Z, camera_eye.X + camera_forward.dX, camera_eye.Y + camera_forward.dY, camera_eye.Z + camera_forward.dZ, camera_up.dX, camera_up.dY, camera_up.dZ);

	light.use();
	
	for(int i = 0; i < objects.size() ; i++)
		objects[i]->displayObject();
	
	if ( displayNormal )
	{
		ShaderManager::changeShader(normal_Shaders);
		for(int i = 0; i < objects.size() ; i++)
		objects[i]->displayNormals();
	}
}

void Scene::loadScene(void)
{
	GLfloat material_Ka[4] = {0.3, 0.05, 0.05, 1};
	GLfloat material_Kd[4] = {1, 1, 1, 1};
	GLfloat material_Ks[4] = {0.3, 0.3, 0.3, 1}; 

	//OBJFile::load(this, "Ressources/OBJ/museumhallRD.obj");
	OBJFile::load(this, "Ressources/Scene4/Scene.obj");
    
	/*OBJFile::load(this, "Ressources/OBJ/cube2.obj");
	myObject3D * myobj1 = objects[0];
	myobj1->material = new Material(material_Ka,material_Kd,material_Ks,100);
	myobj1->material->texture = new myTexture();
	myobj1->material->texture->readTexture("Ressources/TEXTURE/shingles-diffuse.ppm");
	myobj1->material->bump = new myTexture();
	myobj1->material->bump->readTexture("Ressources/TEXTURE/shingles-normal.ppm");*/
	//myobj1->material->cube = new myTexture();
	//myobj1->material->cube->readCube("Ressources/TEXTURE/cube/plus_x.ppm", "Ressources/TEXTURE/cube/moin_x.ppm", "Ressources/TEXTURE/cube/plus_y.ppm", "Ressources/TEXTURE/cube/moin_y.ppm", "Ressources/TEXTURE/cube/plus_z.ppm", "Ressources/TEXTURE/cube/moin_z.ppm");


	for(int i = 0; i < objects.size() ; i++)
	{
		objects[i]->init();
	}
		
}